package com.boost;

import com.boost.handler.BizHandler;
import com.boost.handler.NettyChildHandler;
import com.boost.job.TimeTask;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author Santa
 * @since 2020/3/23
 */
public class DtuStart {

    private static final Logger logger = LoggerFactory.getLogger(DtuStart.class);

    private int port;
    public DtuStart(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try{
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new NettyChildHandler())
            ;
            ChannelFuture sync = bootstrap.bind(port).sync();
            logger.info("service start......");
            sync.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }

    }

    //客户端启动
    public static void main(String[] args) throws Exception {

        //创建定时任务  执行定时召测
        TimeTask timeTask = new TimeTask();
        timeTask.test();
        //启动服务端
        DtuStart chatService = new DtuStart(6666);
        chatService.run();
    }
}























